package com.game.protobuf;


import com.game.cluster.ClusterMessage;
import com.game.net.Connect;
import org.springframework.stereotype.Component;

@Component
@MessageType(1)
public class PingMessageHandler {
    @ProtobufMessage(resp = false, messageType = 1, cmd = 1)
    public static class Ping {
        byte non;
    }

    @ProtobufMessage(resp = true, messageType = 1, cmd = 2)
    public static class Pong {
        long time;

        public Pong(long time) {
            this.time = time;
        }
    }

    @Command(1)
    public void ping(PFSession session, Connect connect) {
        if (session != null) {
            session.send(new Pong(System.currentTimeMillis()));
        } else if (connect != null) {
            Pong pong = new Pong(0L);
            PFMessage pfMessage = new PFMessage(1, 2, ProtostuffUtil.serialize(pong));
            ClusterMessage clusterMessage = new ClusterMessage(pfMessage);
            connect.write(clusterMessage);
        }
    }
}
